package com.mju.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mju.dao.ProductDao;
import com.mju.pojo.Product;
import com.mju.service.IProductService;
import com.mju.service.ex.DeleteException;
import com.mju.service.ex.InsertException;
import com.mju.service.ex.ProductNotFoundException;
import com.mju.service.ex.UpdateException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ProductServiceImpl implements IProductService {
    @Autowired
    private ProductDao productDao;

    @Override
    public List<Product> selectHotList() {
        List<Product> products = productDao.selectHotList();
        for (Product product: products) {
            product.setPriority(null);
            product.setCreatedUser(null);
            product.setCreatedTime(null);
            product.setModifiedUser(null);
            product.setModifiedTime(null);
        }
        return products;
    }

    @Override
    public List<Product> selectNewProductList() {
        List<Product> products = productDao.selectNewProductList();
        for (Product product: products) {
            product.setPriority(null);
            product.setCreatedUser(null);
            product.setCreatedTime(null);
            product.setModifiedUser(null);
            product.setModifiedTime(null);
        }
        return products;
    }

    @Override
    public Product selectById(Integer id) {
        Product product = productDao.selectById(id);
        if(product == null) {
            throw new ProductNotFoundException("商品数据不存在");
        }
        // 将查询结果中的部分属性设置为null
        product.setPriority(null);
        product.setCreatedUser(null);
        product.setCreatedTime(null);
        product.setModifiedUser(null);
        product.setModifiedTime(null);
        return product;
    }

    @Override
    public IPage<Product> queryProductByTitle(Integer currentPage, Integer pageSize, String title) {
        IPage page = new Page<>(currentPage, pageSize);
        // 构建查询构建器，根据标题进行模糊查询
        LambdaQueryWrapper<Product> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Product::getStatus, 1)
                .like(Product::getTitle, title)
                .orderByDesc(Product::getPriority);
        productDao.selectPage(page, lqw);
        return page;
    }

    // 根据类型查询
    @Override
    public IPage<Product> queryProductByType(Integer currentPage, Integer pageSize, String type) {
        IPage<Product> page = new Page<>(currentPage, pageSize);
        LambdaQueryWrapper<Product> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Product::getItemType, type).eq(Product::getStatus, 1);
        productDao.selectPage(page, lqw);
        return page;
    }

    @Override
    public IPage<Product> queryAllProduct(Integer currentPage, Integer pageSize) {
        IPage<Product> page = new Page<>(currentPage, pageSize);
        LambdaQueryWrapper<Product> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Product::getStatus, 1);
        productDao.selectPage(page, lqw);
        return page;
    }

    @Override
    public Integer createProduct(Product product, String username) {
        // 补全日志信息
        Date date = new Date();
        product.setCreatedTime(date);
        product.setCreatedUser(username);
        product.setModifiedTime(date);
        product.setModifiedUser(username);
        // 插入数据
        int rows = productDao.insert(product);
        if(rows != 1) {
            throw new InsertException("创建商品的时候，产生了未知异常");
        }
        return rows;
    }

    @Override
    public Integer deleteProduct(Integer pid) {
        //int rows = productDao.deleteById(pid);
        //if(rows!=1) {
        //    throw new DeleteException("删除异常");
        //}
        //return rows;
        Product product = productDao.selectById(pid);
        product.setStatus(2); // 2：表示删除
        productDao.updateById(product);
        return 1;
    }

    
    @Override
    public Integer updateProduct(Integer pid, Product product) {
        LambdaQueryWrapper<Product> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Product::getId, pid);
        product.setModifiedTime(new Date());
        product.setModifiedUser("管理员");
        int rows = productDao.update(product, lqw);
        if(rows!=1) {
            throw new UpdateException("修改异常");
        }
        return rows;
    }
    @Override
    public Integer getPidByTitle(String title) {
        LambdaQueryWrapper<Product> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Product::getStatus, 1).eq(Product::getTitle, title);

        Product product = productDao.selectOne(lqw);
        return product.getId();
    }
}
